package handlers

import (
	"deliv-mgmt/cache"
	"deliv-mgmt/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func PersonAdd(c *gin.Context) {
	person_code := c.PostForm("person_code")
	personaddress := c.PostForm("person_address")
	persontel := c.PostForm("person_tel")

	data := models.Person{
		PersonCode:    person_code,
		PersonAddress: personaddress,
		PersonTel:     persontel,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no, _ := strconv.Atoi(c.PostForm("order_no"))
	order_name := c.PostForm("order_name")
	order_address := c.PostForm("order_address")
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	data := models.Order{
		OrderNo:      order_no,
		OrderName:    order_name,
		OrderAddress: order_address,
		Pid:          pid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	/*
		SELECT
		  orders.id as OrderId,
		  orders.pid as Pid,
		  orders.order_no as OrderNo,
		  orders.order_name as OrderName,
		  orders.order_address as OrderAddress,
		  people.person_code as PersonCode,
		  people.person_address as PersonAddress,
		  people.person_tel as PersonTel
		FROM
		  people
		  LEFT JOIN orders ON people.id = orders.pid
	*/

	var orderlist []models.OrderList
	redis_key := "order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"orders.id as OrderId",
				"orders.pid as Pid",
				"orders.order_no as OrderNo",
				"orders.order_name as OrderName",
				"orders.order_address as OrderAddress",
				"people.person_code as PersonCode",
				"people.person_address as PersonAddress",
				"people.person_tel as PersonTel").
			Table("people").
			Joins("LEFT JOIN orders ON people.id = orders.pid").
			Offset(Offset).
			Limit(size).
			Find(&orderlist)

		jsonData, _ := json.Marshal(orderlist)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderlist)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  orderlist,
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
		SELECT
		  orders.id AS OrderId,
		  orders.pid AS Pid,
		  orders.order_no AS OrderNo,
		  orders.order_name AS OrderName,
		  orders.order_address AS OrderAddress,
		  people.person_code AS PersonCode,
		  people.person_address AS PersonAddress,
		  people.person_tel AS PersonTel
		FROM
		  `people`
		  LEFT JOIN orders ON people.id = orders.pid
		WHERE
		  orders.id = '3'
	*/

	var orderlist models.OrderList
	redis_key := "order_list_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"orders.id AS OrderId",
				"orders.pid AS Pid",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_address AS OrderAddress",
				"people.person_code AS PersonCode",
				"people.person_address AS PersonAddress",
				"people.person_tel AS PersonTel").
			Table("people").
			Joins("LEFT JOIN orders ON people.id = orders.pid").
			Where("orders.id = ?", id).
			Find(&orderlist)

		jsonData, _ := json.Marshal(orderlist)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderlist)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": orderlist,
	})
	return
}
